#!/usr/bin/perl

@lines = <STDIN>;

%hash = ();

foreach $line (@lines)
{
	chomp($line);
	my($pos,@words) = split(/\s+/,$line);

	if($hash{ $pos } eq "")
	{
		$hash{ $pos } = join(' ',@words);
	}
	else
	{
		$hash{ $pos } .= "___".join(' ',@words);
	}
}

mkdir($ARGV[0], 0777) || print $!;
while ( ($k,$v) = each %hash ) 
{
	$file = $k;

	open(FILE,">$file.strs");
	(@strings) = split(/___/,$v);
	foreach $string (@strings)
	{
		print FILE $string."\n";
	}
	close(FILE);

	$command = "ngram-count -text ".$file.".strs -order 5 -write temp.out";
	system($command);
	$lm = $file.".lm";
	$command = "ngram-count -read temp.out -lm ".$lm;
	system($command);
}
